CLI_FILENAME = nvram

prefix =/usr
exec_prefix =${prefix}
libdir =${exec_prefix}/lib
sharedlibdir =${libdir}
includedir =${prefix}/include

#LIB_VERMAJOR = 0
#LIB_VERMINOR = 1
LIB_FILENAME = libnvram.so
LIB_STAFILENAME = libnvram.a

LIB_CFLAGS  = $(CFLAGS) -shared -fPIC
LIB_LDFLAGS = $(LDFLAGS) -Wl,-soname,$(LIB_FILENAME)

CLI_CFLAGS  = $(CFLAGS)
CLI_LDFLAGS = $(LDFLAGS)

CLI_OBJ = cli.o
LIB_OBJ = crc.o nvram.o

all: cli libnvram

cli: libnvram
	$(CC) $(D_MACRO) $(CLI_CFLAGS) -c -o cli.o cli.c
	#$(CC) $(LIB_FILENAME) -ltds_ext -lpthread -lrt -o $(CLI_FILENAME) $(CLI_LDFLAGS) $(CLI_OBJ) 
	$(CC) -o $(CLI_FILENAME) $(CLI_LDFLAGS) $(CLI_OBJ) $(LIB_STAFILENAME) -lpthread -lrt 

cli.o: cli.c
	$(CC) -fno-omit-frame-pointer  -mapcs-frame $(CLI_CFLAGS) -c -o $@ $<

libnvram:
	$(CC) -fno-omit-frame-pointer $(D_MACRO) $(LIB_CFLAGS) -c -o crc.o crc.c
	$(CC) -fno-omit-frame-pointer $(D_MACRO) $(LIB_CFLAGS) -c -o nvram.o nvram.c
	$(CC) -fno-omit-frame-pointer $(LIB_CFLAGS) $(LIB_LDFLAGS) \
		-o $(LIB_FILENAME) $(LIB_OBJ)
	$(AR) rcs $(LIB_STAFILENAME) $(LIB_OBJ)
	
install:
	-@if [ ! -d $(DESTDIR)$(exec_prefix)  ]; then mkdir -p $(DESTDIR)$(exec_prefix); fi
	-@if [ ! -d $(DESTDIR)$(libdir)       ]; then mkdir -p $(DESTDIR)$(libdir); fi
	-@if [ ! -d $(DESTDIR)$(sharedlibdir) ]; then mkdir -p $(DESTDIR)$(sharedlibdir); fi
	cp $(LIB_STAFILENAME) $(DESTDIR)$(libdir)
	cp libnvram.so $(DESTDIR)$(libdir)
	chmod 644 $(DESTDIR)$(libdir)/$(LIB_STAFILENAME)
	-@($(RANLIB) $(DESTDIR)$(libdir)/libnvram.a || true) >/dev/null 2>&1
	-@if test -n "$(LIB_FILENAME)"; then \
	  cp $(LIB_FILENAME) $(DESTDIR)$(sharedlibdir); \
	  echo "cp $(LIB_FILENAME) $(DESTDIR)$(sharedlibdir)"; \
	  chmod 755 $(DESTDIR)$(sharedlibdir)/$(LIB_FILENAME); \
	  echo "chmod 755 $(DESTDIR)$(sharedlibdir)/$(LIB_FILENAME)"; \
	fi
	-@if [ ! -d $(DESTDIR)$(includedir)   ]; then mkdir -p $(DESTDIR)$(includedir); fi
	cp nvram.h  $(DESTDIR)$(includedir)
	cp sdinitvals.h $(DESTDIR)$(includedir)
	chmod 644 $(DESTDIR)$(includedir)/nvram.h 
	chmod 644 $(DESTDIR)$(includedir)/sdinitvals.h
	

clean:
	rm -f $(CLI_FILENAME) $(LIB_FILENAME)* *.o
